<?php
declare (strict_types = 1);

namespace app\admin\controller;

use Exception;
use think\Request;
use app\model\UserMessage;
use Sffi\ElementPlus\ElForm;
use Sffi\ElementPlus\ElList;
use app\common\entity\ResponseData;
use app\model\SysMessage as ModelSysMessage;
use Sffi\ElementPlus\entity\Option;
use SysMessage;

class Message extends AdminBase
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function list()
    {
        $model = ModelSysMessage::where([]);
        $sum = $model->count();
        $list = $model->hidden(['content_sub'])->select();
        return ResponseData::success($list, ['sum' => $sum]);
    }

    /**
     * 我的消息列表
     */
    public function myMessages($type) {
        $list = UserMessage::scope('unRead')->with(['message' => fn($query) => $query->where('type', $type)])->select();
        return ResponseData::success($list);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function detail($id)
    {
        if(is_int((int)$id) && $id){
            $detail = ModelSysMessage::find($id);
            return ResponseData::success($detail);
        }
        throw new Exception('ID异常');
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function add()
    {
        $data = input('post.');
        $message = ModelSysMessage::create([
            'title' => $data['title'],
            'content' => $data['content'],
            'type' => $data['type'],
            'tags' => $data['tags'],
            'status' => $data['status'],
            'created_by' => $this->request->user['id'],
        ]);
        return ResponseData::success($message);
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function update($id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $res = UserMessage::where(['message_id' => $id])->delete();
        return ResponseData::success($res);
    }

    /**
     * 
     */
    public function getListColumns()
    {
        $list = new ElList();
        $list->addColumn(new ElList\ElInput('标题', 'title', 'title', 120))
            ->addColumn(new ElList\Column('内容', 'content', 'content', 240))
            ->addColumn(new ElList\ElImage('图标', 'icon', 'icon'))
            ->addColumn(new ElList\Column('类型', 'type', 'type_text', 60))
            ->addColumn(new ElList\Column('标签', 'tags', 'tags', 80))
            ->addColumn(new ElList\Column('创建时间', 'create_time', 'create_time', 240))
            ->addColumn(new ElList\Column('状态', 'status', 'status_text'));
        $list->addSearch(new \Sffi\ElementPlus\ElSearch\ElInput('name','name', '请输入角色名称', 0, 31));
        return ResponseData::success($list->__toArray());
    }

    public function getFormColumns()
    {
        $form = new ElForm();
        $iconColumn = new ElForm\ElFile('图标', 'icon', false);
        $iconColumn->setFileType('image')->setSaveType('alioss');
        $types = new ElForm\ElSelect('类型', 'type', '请选择类型');
        $types->setMultiple(false);
        $options = [
                //类型 1通知 2消息 3待办
                ['value' => '1', 'label' => '通知'],
                ['value' => '2', 'label' => '消息'],
                ['value' => '3', 'label' => '待办'],
        ];
        foreach ($options as $option) {
            $types->addOptionByArgs($option['label'], $option['value']);
        }
        $tags = new ElForm\ElSelect('标签', 'tags', '请选择标签');
        $tags->setMultiple(true);
        $tags->setSelectOption(new Option('标签1', "标签"));
        $form->addColumn(new ElForm\ElInput('标题', 'title', 'title'))
            // ->addColumn($tags)
            ->addColumn(new ElForm\ElInput('内容', 'content', 'content'))
            ->addColumn($iconColumn)
            ->addColumn($types);
        $rule = [
            'title' => [
                ['require' => true, 'message' => '请填写角色名称', 'trigger' => 'blur'],
            ],
        ];
        return ResponseData::success($form->__toArray(), ['rule' => $rule]);
    }
}
